package com.chy;

import org.junit.Test;

import java.util.Scanner;

public class 算法题2 {
    @Test
    public void test01(){
        System.out.println(findKth(new int[]{1,3,5,2,2}, 5, 3));
    }
    public int findKth(int[] a, int n, int K) {
        // write code here
        quickSort(a,0,n-1);
        return a[K-1];
    }

    public void 字符个数(String[] args){
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        str = str.toLowerCase();
        Character c = scanner.nextLine().charAt(0);
        int count = 0;
        for(int i=0; i<str.length(); i++){
            if(str.charAt(i) == c || str.toUpperCase().charAt(i) == c)
                count++;
        }
        System.out.println(count) ;
    }
    public void 计算字符串最后一个单词的长度(){
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();

        String[] arr = str.split(" ");
        String world = arr[arr.length - 1];
        System.out.println(world.length()) ;
    }
    public void quickSort(int[] a, int begin, int end){
        if(a.length <= 1 || begin >= end){
            return ;
        }
        int pivotIndex = partition(a, begin, end);
        quickSort(a,begin,pivotIndex-1);
        quickSort(a,pivotIndex+1,end);
    }

    public int partition(int[] a, int begin, int end){
        int pivot = a[end];
        int pivotIndex = begin;

        for(int i = begin; i < end; i++){
            if(a[i] < pivot){
                if(i > pivotIndex){
                    swap(a,i,pivotIndex);
                }
                pivotIndex ++ ;
            }
        }
        swap(a,pivotIndex,end);
        return pivotIndex;
    }

    public void swap(int[] a,int i, int j){
        int temp;
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
}
